//
//  AMDomainNewsViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOMessageListViewController.h"
#import "HOConstants.h"
#import "HOMessageDataModule.h"
#import "HOMessageEntity.h"
#import "HOUtils.h"
#import "Logger.h"
#import "UIImageView+WebCache.h"
#import "HOMessateListTableViewCell.h"
#import "HOReportDetailViewController.h"
#import "HOAddExtraAssistantViewController.h"
#import "HOExtraAssistantEntity.h"

@interface HOMessageListViewController ()
{
	NSMutableDictionary *cellDic;
}
@property(nonatomic, retain)NSArray *messageList;
@end

@implementation HOMessageListViewController
//@synthesize messageType;
//@synthesize messageId;
//@synthesize isLocalMsg;
//@synthesize subType;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	[self initDataSource];
//	[self initTestData];
    [self initTableView];
    [super initEGORefreshTableFooterView];
}
- (void)initDataSource
{
	dontReleaseTableDataList = YES;
	cellDic = [[NSMutableDictionary alloc]init];
	self.tableViewDataList = ((HOMessageDataModule *)dataModule).lastMessageList;
}

//- (void)initTestData
//{
//	self.tableViewDataList = [[NSMutableArray alloc] init];
//	for(int i = 0; i < 10; i ++)
//	{
//		HOMessageEntity *message = [[HOMessageEntity alloc]init];
//		message.Id = i;
//		message.title = @"提现成功!";
//		if(i == 0 || i == 2)
//		{
//			message.detailText = @"恭喜您成功通过我们的等级审核, 恭喜您成功通过我们的等级审核, 恭喜您成功通过我们的等级审核, 恭喜您成功通过我们的等级审核, 恭喜您成功通过我们的等级审核";
//		}
//		else
//		{
//			message.detailText = @"恭喜您成功通过我们的等级审核";
//		}
//	
//		
//		message.time = 1433828394567890;
//		[self.tableViewDataList addObject:message];
//		[message release];
//	}
//}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"消息", @"消息")];
    [baseTableview reloadData];
}
//- (void)rightButtonItemDidClick
//{
//}

- (void)startRequestWithMaxId:(int)maxId
{
    [self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetMyMessages,
                                        kHO_FIELD_MaxID:[self numberWithInt:maxId]}];
}

- (void)pullUpToRefresh
{
	HOMessageEntity *entity = [self.messageList lastObject];
	if(entity == nil)
	{
		[self startRequestWithMaxId:1];
	}
	else
	{
		[self startRequestWithMaxId:entity.messageId];
	}
}

- (void)pullDownToRefresh
{
	[self startRequestWithMaxId:0];
}
- (void)dealloc
{
	self.messageList = nil;
    [super dealloc];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOMessageEntity *message = [self.messageList objectAtIndex:indexPath.section];
	HOMessateListTableViewCell *cell = [cellDic objectForKey:[NSString stringWithFormat:@"%d", message.messageId]];
	if(cell == nil)
	{
		cell = (HOMessateListTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
		[cellDic setObject:cell forKey:[NSString stringWithFormat:@"%d", message.messageId]];
	}
    return [cell getCellHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return [HOUtils defaultOnePixelConversion];
	return 9;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	[self showNoDataView:[self.messageList count] == 0];
	return [self.messageList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 1;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 1;
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOMessageTableViewCellIdentifier = @"HOMessateListTableViewCellIdentifier";
	HOMessateListTableViewCell *messageCell = (HOMessateListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOMessageTableViewCellIdentifier];
	if(messageCell == nil)
	{
		messageCell = [[[HOMessateListTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOMessageTableViewCellIdentifier] autorelease];
		[messageCell initWithWidth:tableView.frame.size.width];
	}
	HOMessageEntity *message = [self.tableViewDataList objectAtIndex:indexPath.section];
	[messageCell setMessage:message];
	int orderId = message.orderId;
	if(orderId == 0)
	{
		
		messageCell.selectionStyle = UITableViewCellSelectionStyleNone;
		messageCell.accessoryType = UITableViewCellAccessoryNone;
	}
	else
	{
		
		messageCell.selectionStyle = UITableViewCellSelectionStyleGray;
		messageCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	return messageCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	HOMessageEntity *message = [self.tableViewDataList objectAtIndex:indexPath.section];
	int orderId = message.orderId;
	if(message.orderType == 2)
	{
		HOAddExtraAssistantViewController *viewCtrl = [[HOAddExtraAssistantViewController alloc] init];
		HOExtraAssistantEntity *entity = [[[HOExtraAssistantEntity alloc] init] autorelease];
		entity.orderId = orderId;
		viewCtrl.companyId = message.companyId;
		viewCtrl.isDetailDisplayModal = YES;
		viewCtrl.extraAssistantEntity = entity;
		
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else
	{
		HOReportDetailViewController *viewCtrl = [[HOReportDetailViewController alloc] init];
		HOReportOrderEntity *entity = [[[HOReportOrderEntity alloc] init] autorelease];
		entity.orderId = orderId;
		viewCtrl.reportType = message.orderType;
		viewCtrl.orderEntity = entity;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
//	return;
//	HOMessageEntity *message = [self.messageList objectAtIndex:indexPath.section];
//	HOMessageDetailViewController *viewCtrl = [[HOMessageDetailViewController alloc] initWithModule:HO_MODULE_TYPE_MESSAGE];
//	viewCtrl.messageEntity = message;
//	[self.navigationController pushViewController:viewCtrl animated:YES];
//	[viewCtrl release];
}

#pragma mark - -- HOViewControllerDelegate impelment
- (void)didReceiveCommand:(NSString *)action data:(id)data
{
    [super didReceiveCommand:action data:data];
    if([action isEqualToString:kHO_CMD_REMOVED_MESSAGE_FROMDB]
       || [action isEqualToString:kHO_CMD_READ_MESSAGE_FROMDB])
    {
        [self reloadData];
        
    }
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetMyMessages])
	{
		[baseTableview reloadData];
	}
}

- (void)reloadData
{
    [baseTableview reloadData];
//    [self updateRefreshViewFrame:[self.dataList count]];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
